/**
 * 修复模块导入问题批处理脚本
 */
const fs = require('fs');
const path = require('path');

console.log('=== 开始修复模块导入问题 ===');

// 修复resourcesStore.js中的导入路径
function fixResourcesStore() {
  console.log('\n>> 修复resourcesStore.js中的导入问题...');
  
  const filePath = path.join(__dirname, '../store/modules/project/resourcesStore.js');
  
  if (!fs.existsSync(filePath)) {
    console.log('   文件不存在:', filePath);
    return;
  }
  
  let content = fs.readFileSync(filePath, 'utf8');
  
  // 检查是否已经修复
  if (content.includes("import { StatisticsRepository } from '../../../../config/services_config';")) {
    console.log('   文件已经修复');
    return;
  }
  
  // 替换引用路径
  content = content.replace(
    "import statisticsRepository from '../../../services/statistics_repository';",
    "import { StatisticsRepository } from '../../../../config/services_config';"
  );
  
  // 替换函数调用
  content = content.replace(
    /await statisticsRepository\./g,
    'await StatisticsRepository.'
  );
  
  fs.writeFileSync(filePath, content, 'utf8');
  console.log('   成功修复resourcesStore.js');
}

// 修复project_edit.js中的导入路径
function fixProjectEdit() {
  console.log('\n>> 修复project_edit.js中的导入问题...');
  
  const filePath = path.join(__dirname, '../projects/plm/pages/project/edit/project_edit.js');
  
  if (!fs.existsSync(filePath)) {
    console.log('   文件不存在:', filePath);
    return;
  }
  
  let content = fs.readFileSync(filePath, 'utf8');
  
  // 检查是否已经修复
  if (content.includes("const { plmProjectStore } = require('../../../../../config/module_config.js').project;")) {
    console.log('   文件已经修复');
    return;
  }
  
  // 替换引用路径
  content = content.replace(
    "const { plmProjectStore } = require('../../../../../store/modules/project/store_config.js');",
    "const { plmProjectStore } = require('../../../../../config/module_config.js').project;"
  );
  
  fs.writeFileSync(filePath, content, 'utf8');
  console.log('   成功修复project_edit.js');
}

// 修复project_detail.js中的导入路径
function fixProjectDetail() {
  console.log('\n>> 修复project_detail.js中的导入问题...');
  
  const filePath = path.join(__dirname, '../projects/plm/pages/project/detail/project_detail.js');
  
  if (!fs.existsSync(filePath)) {
    console.log('   文件不存在:', filePath);
    return;
  }
  
  let content = fs.readFileSync(filePath, 'utf8');
  
  // 检查导入路径
  if (content.includes("import { plmProjectStore } from '../../../../../store/modules/project_store.js';")) {
    // 替换引用路径
    content = content.replace(
      "import { plmProjectStore } from '../../../../../store/modules/project_store.js';",
      "import { plmProjectStore } from '../../../../../config/module_config.js').project;"
    );
    
    fs.writeFileSync(filePath, content, 'utf8');
    console.log('   成功修复project_detail.js');
  } else {
    console.log('   没有找到需要替换的引用路径');
  }
}

// 执行所有修复
try {
  fixResourcesStore();
  fixProjectEdit();
  fixProjectDetail();
  
  console.log('\n=== 模块导入问题修复完成 ===');
} catch (error) {
  console.error('\n修复过程中出错:', error);
}